package com.jiangyg.mall.db.hibernate.transform;

import com.jiangyg.mall.core.utils.HumpUtils;
import org.hibernate.transform.AliasedTupleSubsetResultTransformer;

import java.util.HashMap;
import java.util.Map;

public class AliasToCamelCaseMapResultTransformer extends AliasedTupleSubsetResultTransformer {

	private static final long serialVersionUID = 6246860007202253426L;

	public static final AliasToCamelCaseMapResultTransformer INSTANCE = new AliasToCamelCaseMapResultTransformer();

	@Override
	public Object transformTuple(Object[] tuple, String[] aliases) {
		Map<Object, Object> result = new HashMap<>(tuple.length);
		for (int i = 0; i < tuple.length; i++) {
			String alias = aliases[i];
			if (alias != null) {
				alias = HumpUtils.underlineToCamel(alias.toLowerCase());
				result.put(alias, tuple[i]);
			}
		}
		return result;
	}

	@Override
	public boolean isTransformedValueATupleElement(String[] aliases, int tupleLength) {
		return false;
	}

	protected Object readResolve() {
		return INSTANCE;
	}

}
